package semantic_compute.relation_extraction.yago_matcher;

import common.dao.orient.OrientJDBCDao;
import knowlwdge_graph.knowledge_etl.yago_etl.SchemaConstructer;
import knowlwdge_graph.knowledge_etl.yago_etl.SchemaDefinition;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.Statement;

/**
 * Created by julianzliu on 4/23/2017.
 */
public class YagoRelationSchema {

    static final org.slf4j.Logger logger = LoggerFactory.getLogger(YagoRelationSchema.class);

    public static class YAGO_RELATION_LABEL_CLASS extends SchemaDefinition.COMMON {
        public static String CLASS_NAME = "YAGO_CONCEPT_LABEL_CLASS";
    }

    public static class YAGO_RELATION_GLOSS_CLASS extends SchemaDefinition.COMMON {
        public static String CLASS_NAME = "YAGO_CONCEPT_GLOSS_CLASS";
    }

    public static class YAGO_RELATION_GLOSS_EDGE extends SchemaDefinition.COMMON {
        public static String EDGE_NAME = "YAGO_CONCEPT_GLOSS_EDGE";
    }




    private OrientJDBCDao orientJDBCDao;
    private Connection conn;

    public YagoRelationSchema(){
        this.orientJDBCDao = new OrientJDBCDao();
        this.conn = this.orientJDBCDao.getConn();

    }



    private boolean createSchema(String sql){
        logger.info("[createSchema] sql = {}", sql );
        Statement stmt = null;
        boolean success = true;
        try {
            stmt = this.conn.createStatement();
            success = stmt.execute(sql);
            stmt.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return success;
    }


    public boolean createYAGO_RELATION_LABEL_CLASS(){
        boolean success = true;
        String sql = "";

        String class_name = YAGO_RELATION_LABEL_CLASS.CLASS_NAME;

        sql = String.format("CREATE CLASS %s EXTENDS V", class_name);
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",class_name,YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_ID );
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",class_name,YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_LABEL );
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",class_name,YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_TYPE );
        success &= createSchema(sql);


        sql = String.format("CREATE INDEX %s.%s ON %s(%s) NOTUNIQUE METADATA {ignoreNullValues: true}",
                class_name, "rid",
                class_name, "@rid");
        success &= createSchema(sql);

        sql = String.format("CREATE INDEX %s.%s ON %s(%s) NOTUNIQUE METADATA {ignoreNullValues: true}",
                class_name, YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_ID,
                class_name, YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_ID);
        success &= createSchema(sql);

        sql = String.format("CREATE INDEX %s.%s ON %s(%s) FULLTEXT ENGINE LUCENE METADATA {\n" +
                        "                \"analyzer\": \"org.apache.lucene.analysis.en.EnglishAnalyzer\"\n" +
                        "            }",
                class_name, YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_LABEL,
                class_name, YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_LABEL);
        success &= createSchema(sql);

        return success;
    }

    public boolean createYAGO_RELATION_GLOSS_CLASS(){
        boolean success = true;
        String sql = "";

        String class_name = YAGO_RELATION_GLOSS_CLASS.CLASS_NAME;

        sql = String.format("CREATE CLASS %s EXTENDS V", class_name);
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",class_name,YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_ID );
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",class_name,YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_LABEL );
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",class_name,YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_TYPE );
        success &= createSchema(sql);


        sql = String.format("CREATE INDEX %s.%s ON %s(%s) NOTUNIQUE METADATA {ignoreNullValues: true}",
                class_name, "rid",
                class_name, "@rid");
        success &= createSchema(sql);

        sql = String.format("CREATE INDEX %s.%s ON %s(%s) NOTUNIQUE METADATA {ignoreNullValues: true}",
                class_name, YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_ID,
                class_name, YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_ID);
        success &= createSchema(sql);

        sql = String.format("CREATE INDEX %s.%s ON %s(%s) FULLTEXT ENGINE LUCENE METADATA {\n" +
                        "                \"analyzer\": \"org.apache.lucene.analysis.en.EnglishAnalyzer\"\n" +
                        "            }",
                class_name, YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_LABEL,
                class_name, YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_LABEL);
        success &= createSchema(sql);

        return success;
    }

    public boolean createYAGO_RELATION_GLOSS_EDGE(){
        boolean success = true;
        String sql = "";

        sql = String.format("CREATE CLASS %s EXTENDS E", YAGO_RELATION_GLOSS_EDGE.EDGE_NAME);
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",YAGO_RELATION_GLOSS_EDGE.EDGE_NAME,YAGO_RELATION_GLOSS_EDGE.PROPERTY_YAGO_ID );
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",YAGO_RELATION_GLOSS_EDGE.EDGE_NAME,YAGO_RELATION_GLOSS_EDGE.PROPERTY_YAGO_LABEL );
        success &= createSchema(sql);

        sql = String.format("CREATE PROPERTY %s.%s String",YAGO_RELATION_GLOSS_EDGE.EDGE_NAME,YAGO_RELATION_GLOSS_EDGE.PROPERTY_YAGO_TYPE );
        success &= createSchema(sql);

        //create index YAGO_LABEL.rid on YAGO_LABEL(@rid) NOTUNIQUE METADATA {ignoreNullValues: true}

        sql = String.format("CREATE INDEX %s.%s ON %s(%s) NOTUNIQUE METADATA {ignoreNullValues: true}",
                YAGO_RELATION_GLOSS_EDGE.EDGE_NAME, "rid",
                YAGO_RELATION_GLOSS_EDGE.EDGE_NAME, "@rid");
        success &= createSchema(sql);

        sql = String.format("CREATE INDEX %s.%s ON %s(%s) NOTUNIQUE METADATA {ignoreNullValues: true}",
                YAGO_RELATION_GLOSS_EDGE.EDGE_NAME, YAGO_RELATION_GLOSS_EDGE.PROPERTY_YAGO_ID,
                YAGO_RELATION_GLOSS_EDGE.EDGE_NAME, YAGO_RELATION_GLOSS_EDGE.PROPERTY_YAGO_ID);
        success &= createSchema(sql);


        return success;
    }


    public void create_relation_schema(){

        //TODO
        //createYAGO_CONCEPT_LABEL_CLASS();

        //createYAGO_CONCEPT_GLOSS_CLASS();

        createYAGO_RELATION_GLOSS_EDGE();
    }


    /***********************************************************
     *
     */
    public static void main(String[] args){
        YagoRelationSchema relationSchema = new YagoRelationSchema();

        relationSchema.create_relation_schema();

    }

}
